package com.shujia.homework

import scala.io.Source

object Demo1Studnet {
  def main(args: Array[String]): Unit = {
    /**
     *
     * 1、统计总分年级排名前十学生各科的分数
     */
    //1、读取分数表
    val linesList: List[String] = Source.fromFile("data/score.txt").getLines().toList

    //2、解析数据
    val scoreList: List[(String, String, Int)] = linesList.map((line: String) => {
      val split: Array[String] = line.split(",")
      val id: String = split(0)
      val cid: String = split(1)
      val sco: Int = split(2).toInt
      (id, cid, sco)
    })

    //3、按照学号分组计算总分
    val groupByList: Map[String, List[(String, String, Int)]] = scoreList
      .groupBy { case (id: String, _: String, _: Int) => id }

    //4、计算总分
    val sumScoMap: Map[String, Int] = groupByList.map {
      case (id: String, scos: List[(String, String, Int)]) =>
        //取出分数
        val scoList: List[Int] = scos.map {
          case (_: String, _: String, sco: Int) => sco
        }
        //计算总分
        val sumSco: Int = scoList.sum
        (id, sumSco)
    }
    //将map集合转换list集合
    val sumScoList: List[(String, Int)] = sumScoMap.toList

    //5、按总分排名，取前十
    val sortList: List[(String, Int)] = sumScoList.sortBy {
      case (_: String, sco: Int) => -sco
    }

    //去前十
    val top10: List[(String, Int)] = sortList.take(10)

    //取出学号
    val ids: List[String] = top10.map {
      case (id: String, _: Int) => id
    }

    //过滤取出前十学生各科的分数
    val stop10List: List[(String, String, Int)] = scoreList.filter {
      case (id: String, _: String, _: Int) =>
        ids.contains(id)
    }

    stop10List.foreach(println)

  }

}
